package com.hb.trans.mvp.ui.activity

import android.Manifest
import android.annotation.SuppressLint
import android.location.Location
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.basiclib.base.BaseActivity
import com.basiclib.inter.IPresenter
import com.flyco.tablayout.listener.CustomTabEntity
import com.hb.trans.R
import com.hb.trans.bean.TabEntity
import com.hb.trans.bean.Update
import com.hb.trans.global.ACTIVITY_MAIN
import com.hb.trans.global.HbApplication
import com.hb.trans.greendao.DaoMaster
import com.hb.trans.mvp.contract.MainContract
import com.hb.trans.mvp.presenter.MainPresenter
import com.hb.trans.mvp.ui.fragment.HomeFragment
import com.hb.trans.mvp.ui.fragment.NewsFragment
import com.hb.trans.mvp.ui.fragment.ServeFragment
import com.hb.trans.mvp.ui.fragment.UserCenterFragment
import com.hb.trans.utils.DBUtils
import com.hb.trans.utils.LocationMgr
import com.hb.trans.utils.VersionUpdataHelper
import com.tbruyelle.rxpermissions2.RxPermissions
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.IOException
import javax.inject.Inject


@Route(path = ACTIVITY_MAIN)
class MainActivity : BaseActivity(), MainContract.View {

    override fun setCurrentLocationCity(province: String, city: String, district: String) {
        HbApplication.locCity = city
        HbApplication.locProvince = province
        HbApplication.locDistrict = district
    }

    override fun setGetById(data: Update?) {
        val mVcode = packageManager.getPackageInfo(packageName, 0).versionCode
        val upVCode = data?.app_version!!.toInt()
        if (mVcode < upVCode) {
            VersionUpdataHelper(this, data?.android_url, true)
        }
    }
    @Inject
    lateinit var mPresenter: MainPresenter

    lateinit var builder: StringBuilder
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventLocation(location: Location) {
        mPresenter.getLocationInfo(location)
        LocationMgr.getInstance(this).destory()
    }

    override fun getContentViewResId(): Int {
        return R.layout.activity_main
    }

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun getPresenter(): IPresenter<*>? = mPresenter

    override fun attachView(savedInstanceState: Bundle?) {
//        mapView.onCreate(savedInstanceState)// 此方法必须重写
        mPresenter.attachView(this)
    }


    override fun initView() {
        requestLocationPermission()
        setUpNavBar()

        requestLocationPermission()

//        copyRawDB()
//        selDBData()

    }

//    /**
//     * 方法必须重写
//     */
//    override fun onResume() {
//        super.onResume()
//        mapView.onResume()
//    }
//
//
//    override fun onPause() {
//        super.onPause()
//        mapView.onPause()
//    }
//
//    override fun onSaveInstanceState(outState: Bundle) {
//        super.onSaveInstanceState(outState)
//        mapView.onSaveInstanceState(outState)
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        mapView.onDestroy()
//    }

    private fun selDBData() {

        var mHelper = DaoMaster.DevOpenHelper(this, DBUtils.DB_NAME)
        var db = mHelper.getWritableDatabase()
        var mDaoMaster = DaoMaster(db)
        var daoSchoolSession = mDaoMaster.newSession()
        var user = daoSchoolSession.userDao
        val users  = user.loadAll()

    }

    private fun copyRawDB() {
        try {
            DBUtils.copyDBToDatabases(this@MainActivity)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    @SuppressLint("CheckResult")
    private fun requestLocationPermission() {
        val permission = RxPermissions(this)
        permission.request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
            .subscribe {}

        // 定位
        permission.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
            .subscribe {
                if (it) {
                    LocationMgr.getInstance(this).initLocation()
                } else {
                    AlertDialog.Builder(this)
                        .setTitle("提示")
                        .setMessage("应用需要定位权限或定位未打开")
                        .setPositiveButton("确定") { dialog, _ ->
                            dialog.dismiss()
                        }.show()
                }
            }
    }

    override fun initData() {
//        mPresenter.getById()
    }

    private fun setUpNavBar() {
        val titles = arrayOf("首页", "服务", "资讯", "我的")
        val selectIcons = arrayOf(R.mipmap.ic_home_s, R.mipmap.ic_trans_s, R.mipmap.ic_news_s, R.mipmap.ic_usercenter_s)
        val unSelectIcons =
            arrayOf(R.mipmap.ic_home_un, R.mipmap.ic_trans_un, R.mipmap.ic_news_un, R.mipmap.ic_usercenter_un)
        val tabs = ArrayList<CustomTabEntity>(titles.size)
        for (i in 0 until titles.size) {
            tabs.add(TabEntity(titles[i], selectIcons[i], unSelectIcons[i]))
        }
        val fragments = ArrayList<androidx.fragment.app.Fragment>()
        fragments.add(HomeFragment.newInstance())
        fragments.add(ServeFragment.newInstance())
        fragments.add(NewsFragment.newInstance())
        fragments.add(UserCenterFragment.newInstance())
        tabLayout.setTabData(tabs, this, R.id.frameContainer, fragments)
    }






}